import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class DashboardDefaultScreen extends StatelessWidget {
  final List<Map<String, dynamic>> icons = [
    {'name': '商品管理', 'route': '/spu/list', 'icon': Icons.shopping_bag},
    {'name': '分类管理', 'route': '/category', 'icon': Icons.category},
    {'name': '规格管理', 'route': null, 'icon': Icons.list_alt},
    {'name': '设置', 'route': null, 'icon': Icons.settings},
    {'name': '消息中心', 'route': null, 'icon': Icons.message},
    {'name': '数据统计', 'route': null, 'icon': Icons.analytics},
    {'name': '帮助中心', 'route': null, 'icon': Icons.help},
    {'name': '关于我们', 'route': null, 'icon': Icons.info},
  ];

  final Map<String, dynamic> stats = {
    'todayOrders': 25,
    'todayRevenue': 1250.50,
    'avgOrderValue': 50.02,
    'pendingOrders': 3,
    'topSellingProduct': '纯棉舒适T恤',
    'lowSellingProduct': '休闲短袜三双装',
    'lowStockItems': 2,
  };

  DashboardDefaultScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: AppBar(
        title: const Text('商家工作台'),
        centerTitle: true,
        elevation: 2,
        backgroundColor: theme.primaryColor.withValues(alpha: 0.95),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '今日概览',
                    style: theme.textTheme.titleLarge?.copyWith(
                      fontWeight: FontWeight.bold,
                      color: theme.primaryColor,
                    ),
                  ),
                  const SizedBox(height: 16),
                  GridView.count(
                    crossAxisCount: 2,
                    crossAxisSpacing: 12,
                    mainAxisSpacing: 12,
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    childAspectRatio: 2.5, // 减小宽高比，增加高度
                    children: [
                      _buildStatCard(context, '今日订单数', '${stats['todayOrders']} 单', Icons.shopping_cart),
                      _buildStatCard(context, '今日收入', '¥${stats['todayRevenue'].toStringAsFixed(2)}', Icons.attach_money),
                      _buildStatCard(context, '平均客单价', '¥${stats['avgOrderValue'].toStringAsFixed(2)}', Icons.monetization_on),
                      _buildStatCard(context, '待处理订单', '${stats['pendingOrders']} 单', Icons.pending_actions),
                    ],
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              child: Text(
                '功能入口',
                style: theme.textTheme.titleLarge?.copyWith(
                  fontWeight: FontWeight.bold,
                  color: theme.primaryColor,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: GridView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 4,
                  crossAxisSpacing: 16,
                  mainAxisSpacing: 16,
                  childAspectRatio: 0.75,
                ),
                itemCount: icons.length,
                itemBuilder: (context, index) {
                  final iconData = icons[index];
                  return _buildFunctionCard(context, iconData);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFunctionCard(BuildContext context, Map<String, dynamic> iconData) {
    final theme = Theme.of(context);

    return Card(
      elevation: 1,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: InkWell(
        borderRadius: BorderRadius.circular(12),
        onTap: () {
          if (iconData['route'] != null) {
            context.push(iconData['route']);
          } else {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('${iconData['name']} 功能暂未开放')),
            );
          }
        },
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                  color: theme.primaryColor.withOpacity(0.15),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Icon(
                  iconData['icon'],
                  size: 24,
                  color: theme.primaryColor,
                ),
              ),
              const SizedBox(height: 6),
              Expanded(
                child: Text(
                  iconData['name'],
                  style: theme.textTheme.bodySmall?.copyWith(
                    fontSize: 11,
                    height: 1.2,
                    color: theme.textTheme.bodyLarge?.color,
                  ),
                  textAlign: TextAlign.center,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildStatCard(
      BuildContext context, String title, String value, IconData icon,
      {int maxLines = 1}) {
    final theme = Theme.of(context);

    return Card(
      elevation: 1,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(8.0), // 减小内边距
        child: Row(
          children: [
            Container(
              width: 28, // 减小图标尺寸
              height: 28,
              decoration: BoxDecoration(
                color: theme.primaryColor.withOpacity(0.15),
                borderRadius: BorderRadius.circular(8), // 减小圆角
              ),
              child: Icon(
                icon,
                size: 16, // 减小图标大小
                color: theme.primaryColor,
              ),
            ),
            const SizedBox(width: 8), // 减小间距
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min, // 避免撑满
                children: [
                  Text(
                    title,
                    style: theme.textTheme.bodySmall?.copyWith(
                      fontSize: 10, // 减小字体
                      color: theme.textTheme.bodySmall?.color?.withOpacity(0.7),
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 2),
                  Flexible( // 使用 Flexible 适应剩余空间
                    child: Text(
                      value,
                      style: theme.textTheme.bodyMedium?.copyWith(
                        fontSize: 12, // 减小字体
                        fontWeight: FontWeight.bold,
                        color: theme.textTheme.bodyLarge?.color,
                      ),
                      maxLines: maxLines,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}